#!/usr/bin/env bash

set -eux

template_repos_dir=template/repos

update-template() (
  name="$1"
  dirname="$2"
  subdir="${3:-}"
  dir="${template_repos_dir}/$dirname"
  rm -rf "$dir"
  ourbigbook --generate="$name" --outdir="$dir"
  cd "$dir"
  if [ -n "$subdir" ]; then cd "$subdir"; fi
  npm install
  if [ -n "$subdir" ]; then cd -; fi
  git init
  git add .
  git commit -m 'Create template'
  git remote add origin "git@github.com:cirosantilli/${dirname}.git"
  git push -f
  if [ -n "$subdir" ]; then cd "$subdir"; fi
  npx ourbigbook --publish
  if [ -n "$subdir" ]; then cd -; fi
)

# TODO https://github.com/cirosantilli/ourbigbook/issues/160
update-template subdir ourbigbook-template-subdir docs
update-template min ourbigbook-template-min
update-template default ourbigbook-template
